package com.workmanager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.StudentBean;
import com.workmanager.bean.TeacherBean;
import com.workmanager.service.loginService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Lenovo on 2016/8/29.
 * 当验证码输入有误的时候，会用msg显示出现的原因是验证码输入有误
 */
@RequestMapping("/login")
@Controller
public class loginController {
    @Resource(name="loginService")
    private loginService loginservice;
    @RequestMapping("/check")         //判断验证码是否是正确
    public void check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String securitycode=request.getParameter("securitycode");
        String yanzhengma=(String)request.getSession().getAttribute("rand");
        JSONObject json=new JSONObject();
        securitycode=securitycode.toLowerCase();
        if(yanzhengma!=null){
            yanzhengma=yanzhengma.toLowerCase();
        }
        if(securitycode!=null&&!securitycode.equals(yanzhengma)){            //验证码输入错误
            json.put("jieguo",false);
        }else{
            json.put("jieguo",true);
        }
        response.getWriter().print(json.toJSONString());
    }

    @RequestMapping("/logincheck")//进行用户名和密码的判断，用jquery传递参数
    public void logincheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username=request.getParameter("username");//用js文件传送过来，和在jsp页面中得到是一样的写法
        String password=request.getParameter("password");
        System.out.print("loginController-------logincheck");
        JSONObject json=new JSONObject();
        String status=request.getParameter("status");//获得登录身份

        if(status!=null&&status.equals("teacher")){//如果是教师身份登录
            String name1=loginservice.getTeacherNameByphone(username);
            List<String> teacherlist=loginservice.selectTeacherPhone();//在数据库中得到老师的电话号码的列表
            if(loginservice.teacheralready(teacherlist,username)) {//判断该手机号是否已经注册,true表示已经注册
                if (loginservice.teacherPasswd(loginservice.checkTeacherPasswd(username),password)) {//判断密码是否与数据库中的一致
                    TeacherBean ateacherBean=loginservice.selectaTeacherBean(username);
                    request.getSession().setAttribute("教师",ateacherBean);
                    request.getSession().setAttribute("teachername",ateacherBean.getTeacher_name());
                    json.put("shenfen","teacher");
                    json.put("shifoulogin",true);
                    json.put("shifouzhuce",true);

                } else {
                    json.put("shenfen","teacher");
                    json.put("shifoulogin",false);
                    json.put("shifouzhuce",true);
                }
            }else{
                json.put("shenfen","teacher");
                json.put("shifoulogin",false);
                json.put("shifouzhuce",false);
            }
        }if(status!=null&&status.equals("student")){
            String name2=loginservice.getStudentNamebByPhone(username);//根据学生的账户得到学生的姓名
            List<String> studentlist=loginservice.selectStudentNumber();//得到学生的账户信息list表
            if(loginservice.studentalready(studentlist,username)){//判断该学生信息是否已经注册，true表示已经注册
                if (loginservice.studentPasswd(loginservice.checkStudentPasswd(username), password)) {//判断密码是否与数据库中的一致
                    StudentBean astudentBean=loginservice.selectStudentBean(username);
                    request.getSession().setAttribute("学生",astudentBean);
                    request.getSession().setAttribute("studentname",astudentBean.getStudent_name());
                    json.put("shenfen","student");
                    json.put("shifoulogin",true);
                    json.put("shifouzhuce",true);
                } else {
                    json.put("shenfen","student");
                    json.put("shifoulogin",false);
                    json.put("shifouzhuce",true);
                }

            }else{
                json.put("shenfen","student");
                json.put("shifoulogin",false);
                json.put("shifouzhuce",false);
            }
        }
        response.getWriter().print(json.toJSONString());
    }

    public loginService getLoginservice() {
        return loginservice;
    }

    public void setLoginservice(loginService loginservice) {
        this.loginservice = loginservice;
    }

}
